// Detect the browser
if (navigator.userAgent.indexOf("Opera") != -1
		    && document.getElementById) type = "OP";
if (document.all) type = "IE";
if (document.layers) type = "NN";
if (!document.all && document.getElementById) type = "MO";

function getElement(id)
{
	if (type == "IE") eval("ret = document.all." + id);
	if (type == "NN") eval("ret = document." + id);
	if (type == "MO" || type == "OP")
		eval("ret = document.getElementById('" + id + "')");

	return ret;
}

function login()
{
	window.open("login.php", "Login", "width=640,height=480,status=yes,scrollbars=yes");
}

function edit_player(player)
{
	window.open("edit_player.php?player=" + player, "EditPlayer", "width=640,height=480,status=yes,scrollbars=yes");
}

function edit_squad(game, team)
{
	window.open("edit_squad.php?game=" + game + "&team=" + team, "EditSquad", "width=800,height=800,status=yes,scrollbars=yes");
}

function edit_game(champ, game)
{
	window.open("edit_game.php?champ=" + champ + "&game=" + game, "EditGame", "width=640,height=480,status=yes,scrollbars=yes");
}

function new_game(champ)
{
	window.open("insert_game.php?id=" + champ, "InsertGame", "width=640,height=480,status=yes");
}

function new_stadium(champ, select)
{
	if (select.options[select.selectedIndex].text == '--- New stadium ---')
	{
		window.open("insert_stadium.php?champ=" + champ, "InsertStadium", "width=400,height=250,status=yes");
	}
}

function new_referee(champ, select)
{
	if (select.options[select.selectedIndex].text == '--- New referee ---')
	{
		window.open("insert_referee.php?champ=" + champ, "InsertReferee", "width=400,height=250,status=yes");
	}
}

function new_player_select(player, select)
{
	main_form = document.main_form;

	for (i = 0; i < select.array.length; ++i)
	{
		if (select.array[i][0] > player[0]) break;
	}
	select.array.splice(i, 0, player);
	for (i = 0; i < main_form.elements.length; i++)
	{
		s = main_form.elements[i];
		if (s.home_away == select.home_away)
		{
			for (j = 0; j < s.options.length - 1; ++j)
			{
				if (s.options[j].text > player[0]) break;
			}
			prev = s.options[j];
			s.options[j] = new Option(player[0], player[1] + '/' + player[2]);
			if (s == select) s.options[j].selected = true;
			for (k = j + 1; k < s.options.length; ++k)
			{
				curr = s.options[k];
				s.options[k] = prev;
				prev = curr;
			}
			s.options[k] = prev;
		}
	}
}

function new_player(champ, select, cb)
{
	if (select.options[select.selectedIndex].text == '--- New player ---')
	{
		callback = function (player) { cb(player, select); };

		window.open("insert_player.php?champ=" + champ + "&team=" + select.team_id + "&team_name=" + select.team_name, "InsertPlayer", "width=640,height=480,status=yes");
	}
}

function mark_played()
{
	document.main_form.played[1].setAttribute("checked", true);
	document.main_form.played[1].checked = true;
}

function fill_player_select(champ, select, own, my_players, their_players, my_goals)
{
	while (select.options.length)
		select.options[0] = null;

	if (own.checked)
	{
		curr_array = their_players;
		select.team_id = select.their_id;
		select.team_name = select.their_name;
		select.home_away = select.their_home_away;
	}
	else
	{
		curr_array = my_players;
		select.team_id = select.my_id;
		select.team_name = select.my_name;
		select.home_away = select.my_home_away;
	}

	select.array = curr_array;

	select.onchange = function () { new_player(champ, this, new_player_select) };

	select.options[0] = new Option('--- Not set ---', '0/0');
	for (i = 0; i < curr_array.length; i++)
	{
		select.options[i+1] = new Option(curr_array[i][0], curr_array[i][1] + "/" + curr_array[i][2]);
		if (my_goals[count] != null && curr_array[i][1] == my_goals[count][0])
			select.options[i+1].selected = true;
	}
	select.options[i+1] = new Option('--- New player ---', '0/0');
}

function create_goals(champ)
{
	home = parseInt(document.main_form.home_score.value, 10);
	away = parseInt(document.main_form.away_score.value, 10);
	nrows = home > away ? home : away;

	tbl = getElement("table_result").childNodes[0];
	start_rows = 1;
	childNodes = tbl.childNodes;

	if (nrows == 0)
	{
		while (childNodes.length > nrows + start_rows) {
			tbl.removeChild(childNodes[childNodes.length-1]);
		}
		return;
	}

	if (nrows > 0 && childNodes.length == start_rows)
	{
		row = document.createElement("TR");
		td = document.createElement("TD");
		text = document.createTextNode("Goals:");
		td.appendChild(text);
		row.appendChild(td);
		tbl.appendChild(row);

		row = document.createElement("TR");
		
		td = document.createElement("TD");
		text = document.createTextNode("Player");
		td.appendChild(text);
		row.appendChild(td);

		td = document.createElement("TD");
		text = document.createTextNode("Time");
		td.appendChild(text);
		row.appendChild(td);

		td = document.createElement("TD");
		text = document.createTextNode("Pen");
		td.appendChild(text);
		row.appendChild(td);

		td = document.createElement("TD");
		text = document.createTextNode("Own");
		td.appendChild(text);
		row.appendChild(td);

		td = document.createElement("TD");
		row.appendChild(td);
		td = document.createElement("TD");
		row.appendChild(td);
		td = document.createElement("TD");
		row.appendChild(td);

		td = document.createElement("TD");
		text = document.createTextNode("Player");
		td.appendChild(text);
		row.appendChild(td);

		td = document.createElement("TD");
		text = document.createTextNode("Time");
		td.appendChild(text);
		row.appendChild(td);

		td = document.createElement("TD");
		text = document.createTextNode("Pen");
		td.appendChild(text);
		row.appendChild(td);

		td = document.createElement("TD");
		text = document.createTextNode("Own");
		td.appendChild(text);
		row.appendChild(td);

		tbl.appendChild(row);
	}

	while (childNodes.length > nrows + start_rows + 2)
	{
		tbl.removeChild(childNodes[childNodes.length-1]);
	}

	while (nrows > childNodes.length - start_rows - 2)
	{
		row = document.createElement("TR");
		home_player_td = document.createElement("TD");
		home_minute_td = document.createElement("TD");
		home_pen_td = document.createElement("TD");
		home_own_td = document.createElement("TD");
		away_player_td = document.createElement("TD");
		away_minute_td = document.createElement("TD");
		away_pen_td = document.createElement("TD");
		away_own_td = document.createElement("TD");
		dummy_td1 = document.createElement("TD");
		dummy_td2 = document.createElement("TD");
		dummy_td3 = document.createElement("TD");

		row.appendChild(home_player_td);
		row.appendChild(home_minute_td);
		row.appendChild(home_pen_td);
		row.appendChild(home_own_td);
		row.appendChild(dummy_td1);
		row.appendChild(dummy_td2);
		row.appendChild(dummy_td3);
		row.appendChild(away_player_td);
		row.appendChild(away_minute_td);
		row.appendChild(away_pen_td);
		row.appendChild(away_own_td);

		tbl.appendChild(row);
	}

	for (count = 0; count < childNodes.length - start_rows - 2; count++)
	{
		row = childNodes[count + start_rows + 2];
		if (count < home)
		{
			if (row.childNodes[0].childNodes.length == 0)
			{
				home_select = document.createElement("SELECT");
				home_select.style.width = "100px";
				home_select.name = "home_goal" + count;
				home_select.my_id = HomeId;
				home_select.my_home_away = 'home';
				home_select.my_name = HomeName;
				home_select.their_id = AwayId;
				home_select.their_home_away = 'away';
				home_select.their_name = AwayName;

				home_own = document.createElement("INPUT");
				home_own.setAttribute("type", "checkbox");
				home_own.name = "home_own" + count;
				home_own.select = home_select;
				if (HomeGoals[count] != null && HomeGoals[count][3] == 1)
				{
					home_own.setAttribute("checked", true);
					home_own.checked = true;
				}
				home_own.onclick = function () { fill_player_select(champ, this.select, this, HomePlayers, AwayPlayers, HomeGoals); };

				fill_player_select(champ, home_select, home_own, HomePlayers, AwayPlayers, HomeGoals);

				home_input = document.createElement("INPUT");
				home_input.type = "text";
				home_input.size = 1;
				home_input.name = "home_min" + count;
				if (HomeGoals[count] != null)
					home_input.value = HomeGoals[count][1];

				home_pen = document.createElement("INPUT");
				home_pen.setAttribute("type", "checkbox");
				if (HomeGoals[count] != null && HomeGoals[count][2] == 1)
				{
					home_pen.setAttribute("checked", true);
					home_pen.checked = true;
				}
				home_pen.name = "home_pen" + count;

				row.childNodes[0].appendChild(home_select);
				row.childNodes[1].appendChild(home_input);
				row.childNodes[2].appendChild(home_pen);
				row.childNodes[3].appendChild(home_own);
			}
		}
		else
		{
			if (row.childNodes[0].childNodes.length != 0)
			{
				row.childNodes[0].removeChild(row.childNodes[0].childNodes[0]);
				row.childNodes[1].removeChild(row.childNodes[1].childNodes[0]);
				row.childNodes[2].removeChild(row.childNodes[2].childNodes[0]);
				row.childNodes[3].removeChild(row.childNodes[3].childNodes[0]);
			}
		}

		if (count < away)
		{
			if (row.childNodes[7].childNodes.length == 0)
			{
				away_select = document.createElement("SELECT");
				away_select.name = "away_goal" + count;
				away_select.style.width = "100px";
				away_select.my_id = AwayId;
				away_select.my_home_away = 'away';
				away_select.my_name = AwayName;
				away_select.their_id = HomeId;
				away_select.their_home_away = 'home';
				away_select.their_name = HomeName;

				away_own = document.createElement("INPUT");
				away_own.setAttribute("type", "checkbox");
				away_own.name = "away_own" + count;
				away_own.select = away_select;
				if (AwayGoals[count] != null && AwayGoals[count][3] == 1)
				{
					away_own.setAttribute("checked", true);
					away_own.checked = true;
				}
				away_own.onclick = function () { fill_player_select(champ, this.select, this, AwayPlayers, HomePlayers, AwayGoals); };

				fill_player_select(champ, away_select, away_own, AwayPlayers, HomePlayers, AwayGoals);

				away_input = document.createElement("INPUT");
				away_input.type = "text";
				away_input.size = 1;
				away_input.name = "away_min" + count;
				if (AwayGoals[count] != null)
					away_input.value = AwayGoals[count][1];

				away_pen = document.createElement("INPUT");
				away_pen.setAttribute("type", "checkbox");
				if (AwayGoals[count] != null && AwayGoals[count][2] == 1)
				{
					away_pen.setAttribute("checked", true);
					away_pen.checked = true;
				}
				away_pen.name = "away_pen" + count;

				row.childNodes[7].appendChild(away_select);
				row.childNodes[8].appendChild(away_input);
				row.childNodes[9].appendChild(away_pen);
				row.childNodes[10].appendChild(away_own);
			}
		}
		else
		{
			if (row.childNodes[7].childNodes.length != 0)
			{
				row.childNodes[7].removeChild(row.childNodes[7].childNodes[0]);
				row.childNodes[8].removeChild(row.childNodes[8].childNodes[0]);
				row.childNodes[9].removeChild(row.childNodes[9].childNodes[0]);
				row.childNodes[10].removeChild(row.childNodes[10].childNodes[0]);
			}
		}
	}
}

function fillTeams()
{
	while (document.main_form.home.options.length)
		document.main_form.home.options[0] = null;

	while (document.main_form.away.options.length)
		document.main_form.away.options[0] = null;

	phase = document.main_form.phase.selectedIndex;
	home = 0;
	away = 0;
	for (i = 0; i < Phases[phase].length; ++i)
	{
		document.main_form.home.options[home++] = new Option(Phases[phase][i].text, Phases[phase][i].value);
	}
	for (i = 1; i < Phases[phase].length; ++i)
	{
		document.main_form.away.options[away++] = new Option(Phases[phase][i].text, Phases[phase][i].value);
	}
}

function removeAwayTeam(select)
{
	phase = document.main_form.phase.selectedIndex;
	s = document.main_form.away;
	for (i = 0; i < s.options.length; ++i)
	{
		if (Phases[phase][i].value != s.options[i].value)
			break;
	}
	prev = s.options[i];
	s.options[i] = new Option(Phases[phase][i].text, Phases[phase][i].value);
	for (j = i + 1; j < s.options.length; ++j)
	{
		curr = s.options[j];
		s.options[j] = prev;
		prev = curr;
	}
	s.options[j] = prev;

	s.options[document.main_form.home.options.selectedIndex] = null;
}

function getElementHeight(elem)
{
	if (type == "OP") { 
		xPos = elem.style.pixelHeight;
	} else {
		xPos = elem.offsetHeight;
	}
	return xPos;
}

function getElementWidth(elem) {
	if (type == "OP") {
		xPos = elem.style.pixelWidth;
	} else {
		xPos = elem.offsetWidth;
	}
	return xPos;
}

function getElementsByClass(node, classname) {
    classes = new Array();
	alltags = node.childNodes;
    for (i = 0; i < alltags.length; i++)
        if ((' '+alltags[i].className+' ').indexOf(classname) != -1)
            classes[classes.length] = alltags[i];
    return classes;
}

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 
